import 'package:sprintf/sprintf.dart';
import 'package:mybase/base/tool/empty/FFEmpty.dart';
import 'package:mybase/base/tool/entitylog/MyEntityLog.dart';
import 'package:mybase/base/tool/file/FFDirectory.dart';
import 'package:mybase/base/tool/file/FFile.dart';
import 'dart:core';
import 'dart:io';

class MyApiTestRun {
  static Future<void> createApiCode({
    required String templateFile, //模板目录
    required String targetFile, //目标文件
    required String methodNotes, //方法注释
    required String methodName, //调用方法名
    required String apiMethodType, //ApiUtils.PostMethd
    required String url, //请求的url "v1.token/check"
    required String entityClassName, //实体类名
    String? importHeaderFile, //传入此值，如果目标文件不存在，则会创建目标文件，并写入该文件的内容在顶部，并拼接class XXXX{\n}
  }) async {

    bool isTemplateFileExist = await FFile.isExistFile(templateFile);
    if (isTemplateFileExist == false) {
      print("模板文件不存在 ${templateFile}");
      return;
    }

    bool isTargetFileExist = await FFile.isExistFile(targetFile);
    if (isTargetFileExist == false) {

      if (FFEmpty.stringIsEmpty(importHeaderFile)) {
        print("目标文件不存在 ${targetFile}");
        return;
      }

      File? createTargetFile = await FFile.createFile(targetFile);
      if (createTargetFile == null) {
        print("创建目标文件失败 ${targetFile}");
        return;
      }

      bool isImportHeaderFileExist = await FFile.isExistFile(importHeaderFile!);
      if (isImportHeaderFileExist == false) {
        print("importHeaderFile文件不存在 ${importHeaderFile}");
        return;
      }

      String? importHeaderContent = await FFile.readAsString(importHeaderFile!);
      if (FFEmpty.stringIsEmpty(importHeaderContent)) {
        print("importHeaderFile文件内容为空 ${importHeaderFile}");
        return;
      }

      //准备
      String targetClassName = targetFile.split(Platform.pathSeparator).last;
      targetClassName = targetClassName.split(".").first;
      importHeaderContent = "${importHeaderContent}\n\nclass ${targetClassName} {\n";
      importHeaderContent = "${importHeaderContent}}";

      bool writeImportHeaderContentSuccess = await FFile.writeContentByFile(file: createTargetFile, content: importHeaderContent);
      if (writeImportHeaderContentSuccess == false) {
        print("importHeaderFile内容写入到目标文件失败，请去目标文件Commnd + Z ${targetFile}");
        return;
      }

      print("目标文件创建并写入初始值成功 ${targetFile}");
    }

    if (FFEmpty.stringIsEmpty(methodName)) {
      print("调用方法名methodName不能为空");
      return;
    }

    if (FFEmpty.stringIsEmpty(apiMethodType)) {
      print("请求类型名apiMethodType不能为空");
      return;
    }

    String apiCode = await FFile.readAsString(templateFile);

    if (FFEmpty.stringIsEmpty(apiCode)) {
      print("模板文件内容不应该为空 ${templateFile}");
      return;
    }

    String code = sprintf(apiCode, [
      methodNotes,
      methodName,
      apiMethodType,
      url,
      entityClassName,
    ]);
    // print(code);

    if (FFEmpty.stringIsEmpty(code)) {
      print("模板文件内容不应该为空 ${templateFile}");
      return;
    }

    List<String> addList = code.split("\n");

    if (FFEmpty.listIsEmpty(addList)) {
      print("模板文件内容不应该为空 ${templateFile}");
      return;
    }

    File file = File(targetFile);
    List<String> oldList = await file.readAsLines();
    // print(oldList);

    if (FFEmpty.listIsEmpty(oldList)) {
      print("目标文件内容不应该为空 ${targetFile}");
      return;
    }

    int realLine = -1;
    for (int i = oldList.length - 1; i >= 0; i--) {
        String lineStr = oldList[i];
        if (lineStr == "}") {
          //找到了最后有效的
          realLine = i;
          break;
        }
    }

    if (realLine == -1) {
      print("目标文件内容有效的最后一行不是'}' ${targetFile}");
      return;
    }

    List<String> topList = oldList.sublist(0,realLine);
    List<String> bottomList = oldList.sublist(realLine,oldList.length);

    if (FFEmpty.listIsEmpty(topList) || FFEmpty.listIsEmpty(bottomList)) {
      print("目标文件以最后有效的}分隔错误 ${targetFile}");
      return;
    }

    List<String> writeList = [];
    writeList.addAll(topList);
    writeList.addAll(addList);
    writeList.addAll(bottomList);

    String content = "";
    for (int i = 0; i < writeList.length; i++) {
        content = "${content}\n${writeList[i]}";
    }

    await FFile.removeFile(targetFile);
    bool success = await FFile.writeContentByPath(filePath: targetFile, content: content);
    if (success == true) {
      print("目标文件追加方法成功 ${targetFile}");
    }  else {
      print("目标文件追加方法失败，请去目标文件Commnd + Z, ${targetFile}");
    }

  }
}
